#!/bin/bash

#
# Flow stability for dynamic community detection https://arxiv.org/abs/2101.06131v2
#
# Copyright (C) 2021 Alexandre Bovet <alexandre.bovet@maths.ox.ac.uk>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


# This script runs the computation of autocovariances
# for the case of the APS dataset


TIMESTAMP=$(date +%s)

JNAME="complenet_${TIMESTAMP}"

NCPU=1

# numpy/scipy number of cores
export OPENBLAS_NUM_THREADS=4
export OMP_NUM_THREADS=4

# to use int64 for MKL indexing. To allow more than 2**31 -1 elements
export MKL_INTERFACE_LAYER=ILP64

NETFILENAME="aps_monthly_lcc_net.pickle"

RUNDIR="$HOME/rundir"

DATADIRBASE="$HOME/datadir"

mkdir -p $DATADIRBASE

P1FILE="$DATADIRBASE/p1_vect_complenet/p1_vect_complenet_2000_2010.txt.gz"

LAPINTERTRANSDIR="$DATADIRBASE/lapl_intertransmat"

INTEGDIR="$DATADIRBASE/integralgrid_complenet"

PK_DIR="$DATADIRBASE/pk_complenet"


mkdir -p $LAPINTERTRANSDIR
mkdir -p $INTEGDIR
mkdir -p $PK_DIR


NETNAME="aps_monthly"



TAU_WS=(3650)

# 2010 to 2000
INTEGRAL_INTERVALS=(108 109 110 111 112 113 114 115 116 117 118)

# compute autocov_grid        
python3 -u $RUNDIR/make_sparse_autocov_integral_init_input_forward.py \
        --datadir $LAPINTERTRANSDIR \
        --savedir $INTEGDIR \
        --net_name $NETNAME \
        --p1_file $P1FILE \
        --pk_dir $PK_DIR \
        --ncpu $NCPU \
        --verbose \
        --tol 1e-9 \
        --integral_rtol 1e-9 \
        --print_mem_usage \
        --int_list ${INTEGRAL_INTERVALS[*]} \
        --print_interval 1 \
        --tau_w_list ${TAU_WS[*]} \
        --save_pk_vecs \
        --save_each_step \
        --reverse_time \
        > output_${JNAME}_intg.txt 2> error_${JNAME}_118_to_108_intg.txt
        

# 2000 to 1990
INTEGRAL_INTERVALS=(98 99 100 101 102 103 104 105 106 107 108)


# compute autocov_grid        
python3 -u $RUNDIR/make_sparse_autocov_integral_init_input_forward.py \
        --datadir $LAPINTERTRANSDIR \
        --savedir $INTEGDIR \
        --net_name $NETNAME \
        --pk_init_dir $PK_DIR \
        --pk_dir $PK_DIR \
        --ncpu $NCPU \
        --verbose \
        --tol 1e-9 \
        --integral_rtol 1e-9 \
        --print_mem_usage \
        --int_list ${INTEGRAL_INTERVALS[*]} \
        --print_interval 1 \
        --tau_w_list ${TAU_WS[*]} \
        --save_pk_vecs \
        --save_each_step \
        --reverse_time \
        > output_${JNAME}_intg.txt 2> error_${JNAME}_108_to_98_intg.txt




# 1990 to 1980
INTEGRAL_INTERVALS=(88 89 90 91 92 93 94 95 96 97 98)

python3 -u $RUNDIR/make_sparse_autocov_integral_init_input_forward.py \
        --datadir $LAPINTERTRANSDIR \
        --savedir $INTEGDIR \
        --net_name $NETNAME \
        --pk_init_dir $PK_DIR \
        --pk_dir $PK_DIR \
        --ncpu $NCPU \
        --verbose \
        --tol 1e-9 \
        --integral_rtol 1e-9 \
        --print_mem_usage \
        --int_list ${INTEGRAL_INTERVALS[*]} \
        --print_interval 1 \
        --tau_w_list ${TAU_WS[*]} \
        --save_pk_vecs \
        --save_each_step \
        --reverse_time \
        > output_${JNAME}_intg.txt 2> error_${JNAME}_98_to_88_intg.txt
        

# 1980 to 1970        
INTEGRAL_INTERVALS=(78 79 80 81 82 83 84 85 86 87 88)        


python3 -u $RUNDIR/make_sparse_autocov_integral_init_input_forward.py \
        --datadir $LAPINTERTRANSDIR \
        --savedir $INTEGDIR \
        --net_name $NETNAME \
        --pk_init_dir $PK_DIR \
        --pk_dir $PK_DIR \
        --ncpu $NCPU \
        --verbose \
        --tol 1e-9 \
        --integral_rtol 1e-9 \
        --print_mem_usage \
        --int_list ${INTEGRAL_INTERVALS[*]} \
        --print_interval 1 \
        --tau_w_list ${TAU_WS[*]} \
        --save_pk_vecs \
        --save_each_step \
        --reverse_time \
        > output_${JNAME}_intg.txt 2> error_${JNAME}_88_to_78_intg.txt
